/*
 * @lc app=leetcode.cn id=19 lang=javascript
 *
 * [19] 删除链表的倒数第 N 个结点
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function (head, n) {
    //生成一个临时头节点 防止head被删除
    let temp = new ListNode()
    temp.next = head
    let fast = temp
    //让快指针比满指针多走n步
    while (n != 0) {
        fast = fast.next
        n--
    }
    let slow = temp;
    //同时开始走 在最后一个节点停下
    while (fast.next != null) {
        fast = fast.next;
        slow = slow.next
    }
    slow.next = slow.next.next
    return temp.next

};
// @lc code=end

